package easier.app.x.view;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.EditText;

/**
 * 只在获取焦点是通知变化
 */
public abstract class FocusTextWatcher implements OnFocusChangeListener, TextWatcher {

    private EditText mEditText;

    protected boolean mHasFocus = false;
    protected CharSequence mCharSequence = "";

    public static void setTo( FocusTextWatcher watcher, EditText editText ) {

        if ( watcher.mEditText != null ) {
            watcher.mEditText.removeTextChangedListener( watcher );
            watcher.mEditText.setOnFocusChangeListener( null );
        }

        editText.removeTextChangedListener( watcher );
        editText.addTextChangedListener( watcher );
        editText.setOnFocusChangeListener( watcher );

        watcher.mHasFocus = editText.hasFocus();
        watcher.mCharSequence = editText.getText();
        watcher.mEditText = editText;
    }

    @Override
    public void beforeTextChanged( CharSequence s, int start, int count, int after ) {

    }

    @Override
    public void onTextChanged( CharSequence s, int start, int before, int count ) {

    }

    @Override
    public void afterTextChanged( Editable s ) {

        mCharSequence = s;
        afterFocusTextChanged( mEditText, s, mHasFocus );
    }

    @Override
    public void onFocusChange( View v, boolean hasFocus ) {

        mHasFocus = hasFocus;
        afterFocusTextChanged( mEditText, mCharSequence, hasFocus );
    }

    protected void afterFocusTextChanged( EditText editText, CharSequence s, boolean hasFocus ) {

    }

    public void setEditText( EditText editText ) {

        mEditText = editText;
    }

    public EditText getEditText() {

        return mEditText;
    }

    /**
     * 清除图标
     */
    public static class ClearFocusTextWatcher extends FocusTextWatcher {

        private View mClear;
        private final OnClickListener mClick = new OnClickListener() {
            @Override
            public void onClick( View v ) {

                getEditText().setText( "" );
            }
        };

        public ClearFocusTextWatcher() {

        }

        public ClearFocusTextWatcher( View clear ) {

            setClear( clear );
        }

        public void setClear( View clear ) {

            if ( mClear != null ) {
                mClear.setOnClickListener( null );
            }
            mClear = clear;
            clear.setOnClickListener( mClick );
        }

        public View getClear() {

            return mClear;
        }

        @Override
        protected void afterFocusTextChanged( EditText editText, CharSequence s, boolean hasFocus ) {

            super.afterFocusTextChanged( editText, s, hasFocus );
            /* 有焦点，有内容，显示删除 */
            if ( hasFocus && !TextUtils.isEmpty( s ) ) {
                mClear.setVisibility( View.VISIBLE );
            } else {
                mClear.setVisibility( View.GONE );
            }
        }
    }
}